﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace Example2.View
{
    class View 
    {
        private SpriteBatch     m_spriteBatch;
        private Texture2D       m_texture;
        private KeyboardState   m_oldKeyboardState;
        private float           m_viewScale = 100;

        

        public View(GraphicsDeviceManager a_manager, ContentManager a_contentLoader)
        {
            //Save start key state to detect changes
            m_oldKeyboardState = Keyboard.GetState();

            //this is needed to draw sprites
            m_spriteBatch = new SpriteBatch(a_manager.GraphicsDevice);

            //Load content
            m_texture = a_contentLoader.Load<Texture2D>("player");
            
        }

        internal void DrawLevel(Microsoft.Xna.Framework.Graphics.GraphicsDevice a_graphicsDevice, float a_modelPlayerPosition)
        {
            //draw green background
            a_graphicsDevice.Clear(Microsoft.Xna.Framework.Color.Green);

            //draw all images
            m_spriteBatch.Begin();
            DrawPlayerAt(a_graphicsDevice.Viewport, a_modelPlayerPosition);
            m_spriteBatch.End();
        }

        private void DrawPlayerAt(Viewport a_port, float a_modelPlayerPosition)
        {
            int spriteHeight = (int)m_viewScale;
            int spriteWidth = (int)m_viewScale;
            //Translate from model to view coordinates
            //the model coordinate translates on the left corner of the sprite
            //A model coordinate of 0 translates to the view position of the sprite just outside left side  => -m_viewScale
            //  --------------------
            // _|                  |
            //|@|  a_port.Width    |
            //  |                  |
            //  --------------------
            //A model coordinate of 1 translates to the view position of the sprite just outside right edge => a_port.Width
            //  --------------------
            //  |                  |_
            //  |  a_port.Width    |@|
            //  |                  |
            //  --------------------
            //the scale is the entire movement of the left edge of the sprite
            float scale = a_port.Width + spriteWidth;

            
            int viewSpriteLeftEdge = (int)(a_modelPlayerPosition * scale) - spriteWidth;
            
            //find the center of the viewport and remove half the sprite width
            int viewSpriteTopEdge = a_port.Height / 2 - spriteHeight / 2;

            

            //Create rectangle and draw it
            Rectangle destRect = new Rectangle(viewSpriteLeftEdge, viewSpriteTopEdge, spriteWidth, spriteHeight);
            m_spriteBatch.Draw(m_texture, destRect, Color.White);
        }
    }
}
